/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai.opimage;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderableImage;
import javax.media.jai.CollectionImage;
import javax.media.jai.CollectionImageFactory;
import javax.media.jai.CollectionOp;
import javax.media.jai.JAI;
import javax.media.jai.RenderableCollectionImageFactory;
import ca.forklabs.media.jai.SimpleCollectionImage;
import ca.forklabs.media.jai.operator.ApplyToCollectionDescriptor;

/**
 * Class {@code ApplyToToCollectionRCIF} is a {@link CollectionImageFactory} and
 * {@link RenderableCollectionImageFactory} supporting the
 * <em>applytocollection</em> operation.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.opimage.ApplyToCollectionRCIF">Daniel Léonard</a>
 * @version $Revision$
 */
public class ApplyToCollectionRCIF implements CollectionImageFactory, RenderableCollectionImageFactory {

//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    */
   public ApplyToCollectionRCIF() {
   // nothing
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the name of the operation parameter within the parameter block.
    * @param   pb   the parameter block.
    * @return   the name of the operation.
    */
   protected String getOperation(ParameterBlock pb) {
      int index = ApplyToCollectionDescriptor.OPERATION_PARAMETER_INDEX;
      String operation = (String) pb.getObjectParameter(index);
      return operation;
      }

   /**
    * Gets the parameter block parameter within the parameter block.
    * @param   pb   the parameter block.
    * @return   the parameter block parameter.
    */
   protected ParameterBlock getParameters(ParameterBlock pb) {
      int index = ApplyToCollectionDescriptor.PARAMETERS_PARAMETER_INDEX;
      ParameterBlock parameters = (ParameterBlock) pb.getObjectParameter(index);
      return parameters;
      }

   /**
    * Creates a {@code CollectionImage} that represents the result of applying
    * the requested operation once to each image in the collection.
    * @param   pb   the parameter block.
    * @param   hints   the rendering hints.
    * @return   the collection image containing the results.
    */
   protected CollectionImage apply(ParameterBlock pb, RenderingHints hints) {
      CollectionImage sources = (CollectionImage) pb.getSource(0);

      CollectionImage sinks = new SimpleCollectionImage();

      String operation = this.getOperation(pb);
      ParameterBlock parameters = this.getParameters(pb);

      for (Object source : sources) {
         parameters.removeSources();
         parameters.addSource(source);

         Object sink;
         if (source instanceof RenderedImage) {
            sink = JAI.create(operation, parameters, hints);
            }
         else if (source instanceof RenderableImage) {
            sink = JAI.createRenderable(operation, parameters, hints);
            }
// TODO : remove this, only support rendered and renderable image
         else if (source instanceof CollectionImage) {
            sink = JAI.createCollection(operation, parameters, hints);
            }
         else {
            Class<?> clazz = source.getClass();
            String message = this.getNeitherRenderedNorRenderableErrorMessage(clazz);
            throw new IllegalStateException(message);
            }
         sinks.add(sink);
         }

      return sinks;
      }

   /**
    * Gets the error message saying that a source image is neither a rendered
    * nor a renderable image.
    * @param   clazz   the class of the offending image.
    * @return   the error message.
    */
   protected String getNeitherRenderedNorRenderableErrorMessage(Class<?> clazz) {
      String key = Resources.NEITHER_RENDERED_NOR_RENDERABLE;
      String message = Resources.getLocalizedString(key, clazz);
      return message;
      }


//---------------------------
// Implemented methods from javax.media.jai.CollectionImageFactory
//---------------------------

   /**
    * Calls {@link #apply(ParameterBlock, RenderingHints)}.
    * @param   pb   the parameter block.
    * @param   hints   the rendering hints.
    * @return   the collection image containing the results.
    */
   public CollectionImage create(ParameterBlock pb, RenderingHints hints) {
      CollectionImage image = this.apply(pb, hints);
      return image;
      }

   /**
    * It is impratical to perform the update.
    * @param old_pb   ignored
    * @param old_hints   ignored
    * @param new_pb   ignored
    * @param new_hints   ignored
    * @param old_image   ignored
    * @param op   ignored
    * @return always {@code null}.
    */
   public CollectionImage update(ParameterBlock old_pb, RenderingHints old_hints,
                                 ParameterBlock new_pb, RenderingHints new_hints,
                                 CollectionImage old_image,
                                 CollectionOp op) {
   // it is impracticable to perform the update
      CollectionImage new_image = null;
      return new_image;
      }


//---------------------------
// Implemented methods from javax.media.jai.RenderableCollectionImageFactory
//---------------------------

   /**
    * Calls {@link #apply(ParameterBlock, RenderingHints)}.
    * @param   pb   the parameter block.
    * @return   the collection image containing the results.
    */
   @Override
   public CollectionImage create(ParameterBlock pb) {
      RenderingHints hints = null;
      CollectionImage image = this.apply(pb, hints);
      return image;
      }

   }
